package com.yuexun.dhdoctor.module.departmentDocumentNew.adapter;

import android.view.View;
import android.widget.AbsListView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.baselistener.OnItemPartClickListener;
import com.yuexun.dhdoctor.baselistener.OnItemPartClickListener02;
import com.yuexun.dhdoctor.module.departmentDocumentNew.bean.MemberListBean;
import com.yuexun.dhdoctor.module.departmentDocumentNew.bean.ShareDocBean;


import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.Collection;
import java.util.List;


/**
 * Created by wangt on 2017/7/11.
 */

public class DocMyShareAdapter extends KJAdapter<ShareDocBean> {

    private OnItemPartClickListener02 listener;

    boolean isDo;

    public void setDo(boolean aDo) {
        isDo = aDo;
        notifyDataSetChanged();
    }

    public void SetOnItemPartClickListener(OnItemPartClickListener02 listener) {
        this.listener = listener;
    }


    public DocMyShareAdapter(AbsListView view, Collection<ShareDocBean> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
    }

    @Override
    public void convert(AdapterHolder helper, final ShareDocBean item, boolean isScrolling, final int position) {
        LinearLayout ll = helper.getView(R.id.ll_doc_my);
        TextView title = helper.getView(R.id.tv_doc_my_title);
        TextView name = helper.getView(R.id.tv_doc_my_name);
        TextView set = helper.getView(R.id.tv_doc_my_set);
        CheckBox selectView = helper.getView(R.id.checkbox_doc_my);
        title.setText(item.getDocName());

        List<MemberListBean> memberList = JSON.parseArray(item.getMemberList(), MemberListBean.class);
        String nameList = "";
        for (int i = 0; i < memberList.size(); i++) {
            if (i == 10) {
                break;
            }
            nameList = nameList + memberList.get(i).getMemeberName() + "、";
        }
        nameList = nameList.substring(0, nameList.length() - 1);
        name.setText(nameList);


        if (isDo) {
            selectView.setVisibility(View.VISIBLE);
            ll.setClickable(true);
            ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (item.isSelect()) {
                        item.setSelect(false);
                    } else {
                        item.setSelect(true);
                    }
                    notifyDataSetChanged();
                }
            });
        } else {
            selectView.setVisibility(View.GONE);
            ll.setClickable(false);
        }

        selectView.setChecked(item.isSelect());

        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.OnClick(position);
            }
        });
    }
}
